package org.example.Server.service.sruvey.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.Server.mapper.paper.PaperPersonalMapper;
import org.example.Server.mapper.survey.SurveyMapper;
import org.example.Server.model.examination.qs.QSSubmitDTO;
import org.example.Server.model.paper.PaperHeader;
import org.example.Server.model.survey.*;
import org.example.Server.service.sruvey.SurveyService;
import org.example.Server.utils.ResultAPI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

import static org.example.Server.utils.ExceptionEnum.SQL_ERROR;

@Service
public class SurveyServiceImpl extends ServiceImpl<SurveyMapper, Survey> implements SurveyService {
    @Autowired
    SurveyMapper surveyMapper;
    @Autowired
    PaperPersonalMapper paperPersonalMapper;

    @Override
    public ResultAPI getSurveyList(Integer techId) {
        List<SurveyVO> surveyVOS = surveyMapper.getSurveyList(techId);
        return ResultAPI.success(surveyVOS);
    }

    @Override
    public ResultAPI getStuSurveyList(Integer userId, Integer semId, Integer status) {
        List<SurveyVO> surveyVOS = surveyMapper.getStuSurveyList(userId, semId, status);
        return ResultAPI.success(surveyVOS);
    }

    /**
     * 获取学生调查问卷作答
     *
     * @param surveyStuDTO
     * @return
     */
    @Override
    public ResultAPI getStuSurvey(SurveyStuDTO surveyStuDTO) {
        {
            Integer surveyStuId = surveyStuDTO.getSurvey_stu_id();
            Integer paperId = surveyStuDTO.getPaper_id();
            Integer surveyId = surveyStuDTO.getSurvey_id();
            SurveyStuVO surveyStuVO = new SurveyStuVO();
            try {
                //获取试卷头
                PaperHeader paperHeader = paperPersonalMapper.getPaperHeaderById(paperId);
                surveyStuVO.setPaperHeader(paperHeader);
                //获取选择题
                List<SurveyStuSel> surveyStuSels = surveyMapper.getSurStuSels(paperId, surveyStuId);
                //获取问答题
                List<SurveyStuQna> surveyStuQnas = surveyMapper.getSurStuQnas(paperId, surveyStuId);

                surveyStuVO.getQsList().add(surveyStuQnas);
                surveyStuVO.getQsList().add(surveyStuSels);
                return ResultAPI.success(surveyStuVO);
            } catch (Exception e) {
                log.error("获取答题详情失败", e);
                return ResultAPI.error(SQL_ERROR.getResultCode(), SQL_ERROR.getResultMsg());
            }
        }
    }

    /**
     * 学生提交问卷
     *
     * @param surveyStuDTO
     * @return
     */
    @Transactional
    @Override
    public ResultAPI addStuSurvey(SurveySubmitDTO surveyStuDTO) {
        //判断一下有没有已经提交了
        Integer flag = surveyMapper.getStuSurveyExist(surveyStuDTO);
        if (flag != null) {
            return ResultAPI.error("该学生已提交！");
        }
        List<QSSubmitDTO> questions = surveyStuDTO.getQuestions();
        surveyMapper.addStuSurveySubmit(surveyStuDTO);
        Integer surveyStuId = surveyStuDTO.getSurvey_stu_id();
        // 更新stu_qs_sel
        List<QSSubmitDTO> selList = questions.stream().filter(item -> !Objects.equals(item.getQs_type(), "qna")).toList();
        if (!selList.isEmpty()) {
            surveyMapper.addStuSurveySelSubmit(selList, surveyStuId);
        }
        // 更新stu_qs_qna
        List<QSSubmitDTO> qnaList = questions.stream().filter(item -> Objects.equals(item.getQs_type(), "qna")).toList();
        if (!qnaList.isEmpty()) {
            surveyMapper.addStuSurveyQnaSubmit(qnaList, surveyStuId);
        }
        return ResultAPI.success(surveyStuDTO.getSurvey_stu_id());
    }
}
